﻿using System;
using System.IO;
using System.Collections;
using System.Data;
using System.Xml;
using System.Configuration;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CVCommunity.Provider;
using CVCommunity.Kernel;
using CVCommunity.Kernel.DALFactory;
using CVCommunity.Kernel.Handlers;
using CVCommunity.Kernel.Entity;
public class Detect : Page
{
    private void Page_Load(object sender, EventArgs e)
    {
    	DetectAll();
    }

    private void DetectAll()
    {
        bool pass = true;

        string[] machineCfgPathArr = HttpRuntime.MachineConfigurationDirectory.Split('\\');
        string CLRVersion = machineCfgPathArr[machineCfgPathArr.Length - 2];

        Response.Write("<p>系统要求环境为 microsoft .net framework 2.0</p>");

        if (CLRVersion.IndexOf("v2.0") == -1)
        {
            pass = false;
            Response.Write("<p style='color:#ff0000'>检测失败：当前版本为microsoft .net framework " + CLRVersion + "</p>[<a href='CVCommunity安装手册.html' target='_blank'>查看安装手册</a>]");
        }
        else
        {
            Response.Write("<p>检测通过</p>");
        }

        try
        {
            Response.Write("检测配置文件的访问权限");
            Settings.Rewriter.Save();
            Settings.Attach.Save();
            Settings.Basis.Save();
            Settings.Service.Save();
            Settings.Statistic.Save();
            Settings.Style.Save();
            Settings.Tax.Save();
            Settings.Time.Save();
            Settings.Verifier.Save();
            Response.Write("<p>检测通过</p>");
        }
        catch { 
	try
        {
            Response.Write("检测配置文件的访问权限");
            Settings.Rewriter.Save();
            Settings.Attach.Save();
            Settings.Basis.Save();
            Settings.Service.Save();
            Settings.Statistic.Save();
            Settings.Style.Save();
            Settings.Tax.Save();
            Settings.Time.Save();
            Settings.Verifier.Save();
            Response.Write("<p>检测通过</p>");
        }
        catch { pass = false; Response.Write("<p style='color:#ff0000'>拒绝访问：请给 settings/ 目录分配写权限。[<a href='CVCommunity安装手册.html' target='_blank'>查看安装手册</a>]</p>"); }

	}

        string provider, dblink, dal;
        if (Fetch.Get("dbtype") == "access")
        {
            provider = "CVCommunity.Provider.OleDbFactory, CVCommunity.Provider";
            dal = "CVCommunity.Access";
            dblink = "//data/db.ascx";

        }
        else
        {
            string module = "Data Source={0}; Database={1}; UID={2}; Password={3};";
            dblink = string.Format(module, Fetch.Get("server"), Fetch.Get("dbname"), Fetch.Get("uid"), Fetch.Get("pwd"));
            provider = "CVCommunity.Provider.SqlDbFactory, CVCommunity.Provider";
            dal = "CVCommunity.SqlServer";
        }

        Response.Write("<p>检测数据库连接</p>");
        Connection conn = null;
        try
        {
            conn = new Connection(new DbProvider(provider, dblink));
            conn.Open();
            conn.Close();
            Response.Write("<p>检测通过</p>");
        }
        catch (Exception e)
        {
            pass = false;
            if (conn != null)
                conn.Close();
            Response.Write("<p style='color:#ff0000'>" + e.Message + "[<a href='CVCommunity安装手册.html' target='_blank'>查看安装手册</a>]</p>");
        }

        Response.Write("<p>检测数据访问程序集</p>");
        if (File.Exists(Request.MapPath("bin/" + dal + ".dll", "/", false)))
        {
            Response.Write("<p>检测通过</p>");
        }
        else
        {
            pass = false;
            Response.Write("<p style='color:#ff0000'>程序集 " + dal + ".dll 不在 bin 包下。请确保 bin 包在应用程序根目录下。</p>");
        }

        Response.Write("<p>试图访问数据库</p>");
        try
        {
            conn.ExecuteNonQuery(@"CREATE TABLE [TTMMPP](Id int NOT NULL)");
            conn.ExecuteNonQuery(@"INSERT INTO [TTMMPP](Id) VALUES(0)");
            conn.ExecuteNonQuery(@"DROP TABLE [TTMMPP]");
        }
        catch (Exception e)
        {
            pass = false;
            if (conn != null)
                conn.Close();
            Response.Write("<p style='color:#ff0000'>" + e.Message + "</p>[<a href='CVCommunity安装手册.html' target='_blank'>查看安装手册</a>]");
        }

        if (!pass)
        {
            Response.Write("<br /><br /><br /><p style='color:#ff0000'>无法通过安装检测，安装终止！请认真阅读 [<a href='CVCommunity安装手册.html' target='_blank'>查看安装手册</a>] 后再继续！</p>");
        }
        else
        {
            Response.Write("<br /><br /><br /><p style='color:#00ff00'>通过安装检测，您可以继续安装！</p>");
        }

    }

    private void ErrorMessage(string s)
    {
        Response.Write(s);
        Response.End();
    }

    protected override void OnInit(EventArgs e)
    {
        InitializeComponent();
        base.OnInit(e);
    }

    private void InitializeComponent()
    {
        this.Load += new EventHandler(this.Page_Load);
    }
}
